package view;

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

import model.*;
import control.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewSpecie extends JPanel {
  public static final String NAME = "specie";
  private SnakesCRUD parent;
  
  private ArrayList<Specie> species;
  private ArrayList<Genus> genus;
  private ArrayList<Biologist> biologists;
  
  private CSpecies specieControl;
  
  public NewSpecie(SnakesCRUD parent) {
    this.parent = parent;
    
    species = new ArrayList<Specie>();
    genus = new ArrayList<Genus>();
    biologists = new ArrayList<Biologist>();
    specieControl = new CSpecies(parent.getConnection());
    
    initComponents();
    updateGenus();
    updateBiologist();
    updateTable();
  }
  
  private final void updateTable() {

      try {
		this.species = specieControl.retrieveAll();
		
		Object[] o = new Object[5];
	      ((DefaultTableModel)(tbSpecies.getModel())).setRowCount(0);
	      
	      for (int i = 0; i < species.size(); i++) {
	        o[0] = species.get(i).getScientificName();
	        o[1] = species.get(i).getUsualName();
	        o[2] = species.get(i).getObservations();
	        o[3] = species.get(i).getGenus().getName();
	        o[4] = species.get(i).getBiologist().getName();
	        
	        ((DefaultTableModel)(tbSpecies.getModel())).addRow(o);
	      }
	} catch (SQLException e) 
	{
		JOptionPane.showMessageDialog(null, e.getMessage());
	} catch (ClassNotFoundException e) {
		JOptionPane.showMessageDialog(null, e.getMessage());
	}
  }
  
  private final void updateGenus() 
  {
    try {
      this.genus = new CGenus(parent.getConnection()).retrieveAll();
      
      ((DefaultListModel)(ltGenus.getModel())).clear();
      
      for (int i = 0; i < genus.size(); i++){
    	((DefaultListModel)(ltGenus.getModel())).addElement(genus.get(i).getName());
      }
      
      if (genus.size() == 0)
        JOptionPane.showMessageDialog(null, "There is no data on GENUS table!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
    catch(Exception e) {}
  }
  
  private final void updateBiologist() 
  {
    try {
      this.biologists = new CBiologist(parent.getConnection()).retrieveAll();
      
      ((DefaultListModel)(ltBiologist.getModel())).clear();
      
      for (int i = 0; i < biologists.size(); i++){
    	((DefaultListModel)(ltBiologist.getModel())).addElement(biologists.get(i).getName());
      }
      
      if (biologists.size() == 0)
        JOptionPane.showMessageDialog(null, "There is no data on BIOLOGIST table!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
    catch(Exception e) {
    	JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
    }
  }
  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        {
        	txtUsualName = new javax.swing.JTextField();
        }
        {
        	jScrollPane5 = new JScrollPane();
        }
        jLabel4 = new javax.swing.JLabel();
        {
        	jScrollPane4 = new JScrollPane();
        	{
        		txaObs = new javax.swing.JTextArea();
        		jScrollPane4.setViewportView(txaObs);
        	}
        }
        jLabel8 = new javax.swing.JLabel();
        GroupLayout layout = new GroupLayout((JComponent)this);
        this.setLayout(layout);
        this.setPreferredSize(new java.awt.Dimension(782, 479));
        txtName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ltGenus = new javax.swing.JList();
        ltBiologist = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        {
        	lblBiologist = new JLabel();
        	lblBiologist.setText("Biologist:");
        }
        tbSpecies = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Editing MEMBER Table");

        jLabel2.setText("Scientific Name:");

        jLabel3.setText("Usual Name:");

        jLabel4.setText("Observations:");

        jLabel8.setText("Genus:");
        
        ltBiologist.setModel(new DefaultListModel());
        jScrollPane5.setViewportView(ltBiologist);

        ltGenus.setModel(new DefaultListModel());
        jScrollPane1.setViewportView(ltGenus);
        ltGenus.setPreferredSize(new java.awt.Dimension(117, 128));

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap(24, 24)
        	.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(jScrollPane2, 0, 96, Short.MAX_VALUE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createParallelGroup()
        	        .addComponent(jScrollPane5, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
        	        .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE))
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(layout.createSequentialGroup()
        	                .addGap(13)
        	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	                    .addComponent(txtName, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	                .addComponent(jLabel8, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(lblBiologist, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	            .addComponent(txtUsualName, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addGap(23)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(53))
        	            .addComponent(jScrollPane4, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE))
        	        .addGap(40)))
        	.addGap(29)
        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(btnRefresh, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnRemove, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnModify, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnAdd, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addContainerGap(50, 50));
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap(21, 21)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(layout.createSequentialGroup()
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jLabel4, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(6)))
        	                .addGap(8)
        	                .addGroup(layout.createParallelGroup()
        	                    .addGroup(layout.createSequentialGroup()
        	                        .addComponent(jScrollPane4, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE))
        	                    .addGroup(layout.createSequentialGroup()
        	                        .addComponent(txtUsualName, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)))
        	                .addGap(0, 0, Short.MAX_VALUE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
        	                .addComponent(txtName, 0, 209, Short.MAX_VALUE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(btnAdd, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(28)))
        	                .addGap(23)
        	                .addComponent(btnRemove, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
        	                .addGap(0, 16, Short.MAX_VALUE)))
        	        .addGap(8)
        	        .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(8)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
        	                .addGap(8)
        	                .addComponent(lblBiologist, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addGap(20)
        	                .addComponent(btnModify, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
        	                .addGap(52)))
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jScrollPane5, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
        	                .addGap(7))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addPreferredGap(jScrollPane5, btnRefresh, LayoutStyle.ComponentPlacement.INDENT)
        	                .addComponent(btnRefresh, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE))))
        	    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	        .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 706, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 7, Short.MAX_VALUE)))
        	.addContainerGap(48, 48));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        tbSpecies.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {
                "Name", "Observations", "Description", "Family"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbSpecies);

    }// </editor-fold>//GEN-END:initComponents

  private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
    if (ltGenus.getSelectedIndex() != -1 && ltBiologist.getSelectedIndex() != -1) 
    {  
      try {
       
    	Specie specie = new Specie();
    	specie.setBiologist(biologists.get(ltBiologist.getSelectedIndex()));
    	specie.setGenus(genus.get(ltGenus.getSelectedIndex()));
    	specie.setObservations(txaObs.getText());
    	specie.setScientificName(txtName.getText());
    	specie.setUsualName(txtUsualName.getText());
  
        specieControl.create(specie);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnAddActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    if (!txtName.getText().isEmpty()) {
      try {
    	  Specie specie = new Specie();
    	  specie.setScientificName(txtName.getText());
    
        specieControl.delete(specie);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnRemoveActionPerformed

  private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
        if (ltGenus.getSelectedIndex() != -1 && ltBiologist.getSelectedIndex() != -1) 
    {  
      try {
    	  Specie specie = new Specie();
      	specie.setBiologist(biologists.get(ltBiologist.getSelectedIndex()));
      	specie.setGenus(genus.get(ltGenus.getSelectedIndex()));
      	specie.setObservations(txaObs.getText());
      	specie.setScientificName(txtName.getText());
      	specie.setUsualName(txtUsualName.getText());
    
        specieControl.update(specie);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnModifyActionPerformed

  private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
    updateGenus();
    updateBiologist();
    updateTable();
  }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnModify;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnRemove;
    private javax.swing.JTextArea txaObs;
    private javax.swing.JTextField txtName;
    private JScrollPane jScrollPane5;
    private JLabel lblBiologist;
    private javax.swing.JTextField txtUsualName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList ltGenus;
    private javax.swing.JList ltBiologist;
    private javax.swing.JTable tbSpecies;
    private JScrollPane jScrollPane4;
    // End of variables declaration//GEN-END:variables
}
